package red.sms.db.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;

import red.sms.db.config.DBConfig;
import red.sms.db.entity.SmsMT;
import red.sms.util.IConstant;
import red.sms.util.Logger;

public class SmsMtDAO {
	public static DBConfig dbc = new DBConfig();

	// insert method
	public static int enqueue(int requestId, String phoneNumber,
			String commandCode, String maDichVu, String info) {
		int result = IConstant.ENQUEUE.SUCCESS;
		try {
			java.util.Date date= new java.util.Date();
			String insertSQL = "INSERT INTO sms_mt(request_id, phone_number, command_code, ma_dich_vu, info, update_date)"
					+ "VALUES (?, ?, ?, ?, ?, ?)";
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = ConnectionFactory.getInstance().getConnection();
			PreparedStatement ps = con.prepareStatement(insertSQL);
			ps.setString(1, String.valueOf(requestId));
			ps.setString(2, phoneNumber);
			ps.setString(3, commandCode);
			ps.setString(4, maDichVu);
			ps.setString(5, info);
			ps.setTimestamp(6, new Timestamp(date.getTime()));
			ps.executeUpdate();
			con.close();
		} catch (Exception ex) {
			Logger.log("\n" + ex.getMessage(), true);
			result = IConstant.ENQUEUE.FAIL;
		}
		return result;
	}

	public static boolean hasMessage() {
		int count = 0;
		try {
			String countSQL = "SELECT COUNT(*) FROM SMS_MT WHERE active_flag = 1";
			Connection con = ConnectionFactory.getInstance().getConnection();
			Statement stmt = con.createStatement();
			ResultSet result = stmt.executeQuery(countSQL);
			result.next();
			count = result.getInt(1);
			con.close();
		} catch (Exception ex) {
			Logger.log("\n" + ex.getMessage(), true);
		}
		if (count > 0)
			return true;
		else
			return false;
	}

	// getFirstRecord
	public static SmsMT dequeue() {
		try {
			Connection con = ConnectionFactory.getInstance().getConnection();
			Statement stmt = con.createStatement();
			ResultSet result = stmt
					.executeQuery("SELECT * FROM sms_mt where active_flag = 1 ORDER BY update_date DESC LIMIT 1");
			SmsMT smsMt = new SmsMT();
			while (result.next()) {
				smsMt.setMtId((result.getInt("mt_id")));
				smsMt.setRequestId(result.getString("request_id"));
				smsMt.setInfo(result.getString("info"));
				smsMt.setMaDichVu(result.getString("ma_dich_vu"));
				smsMt.setPhoneNumber(result.getString("phone_number"));
				smsMt.setCommandCode(result.getString("command_code"));
			}

			if (smsMt.getRequestId() != null) {
				String updateSql = "UPDATE  sms_mt SET active_flag = 0 WHERE request_id = "
						+ smsMt.getRequestId();
				PreparedStatement ps = con.prepareStatement(updateSql);
				ps.executeUpdate();
			}

			con.close();
			return smsMt;
		} catch (Exception ex) {
			ex.printStackTrace();
			Logger.log("\n" + ex.getMessage(), true);
			return null;
		}
	}

	// delete method
	public static void Delete(String request_id) {
		try {
			String deleteSQL = "DELETE from sms_mt WHERE request_id = ?";
			Connection con = ConnectionFactory.getInstance().getConnection();
			PreparedStatement ps = con.prepareStatement(deleteSQL);
			ps.setString(1, request_id);
			ps.executeUpdate();
			con.close();
		} catch (Exception ex) {
			Logger.log("\n" + ex.getMessage(), true);
		}
	}


}
